package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2023 12 20 16 25
 **/
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    /**
     * 列表
     * 条件查询
     * 分页
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult findAdChannel(ChannelDto channelDto) {
        //判断参数是否存在
        if(null==channelDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //设置分页参数
        channelDto.checkParam();

        QueryWrapper<AdChannel> adChannelQueryWrapper = new QueryWrapper<>();
        if(channelDto.getName()!=""&&channelDto.getName()!=null){
            adChannelQueryWrapper.lambda().like(AdChannel::getName,channelDto.getName());
        }

        Page<AdChannel> adChannelPage = new Page<>(channelDto.getPage(),channelDto.getSize());


        IPage<AdChannel> adChannelIPage = this.baseMapper.selectPage(adChannelPage, adChannelQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(
                channelDto.getPage()
                ,channelDto.getSize()
                ,(int)adChannelIPage.getTotal()
        );

        pageResponseResult.setData(adChannelIPage.getRecords());


        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        if(null==adChannel){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        adChannel.setCreatedTime(new Date());
        save(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult updateAdchannel(AdChannel adChannel){
        if(null==adChannel){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        this.baseMapper.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),AppHttpCodeEnum.SUCCESS.getErrorMessage());

    }

    @Override
    public ResponseResult delAdchannel(Integer id) {
        if(null==id){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel adChannel = getById(id);

        if(null==adChannel){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NOT_EXIST);
        }

        removeById(id);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
